﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using DAL;
using CMSModel;

namespace BLL
{
    /// <summary>
    /// 股票基本信息导入
    /// </summary>
    public class StockInfoImport
    {
        public StockInfoImport()
        {

        }

        //第一步导入tb_public_0007表
        //根据编号再导入tb_public_0001表
        //再导入相关的tb_company_0002表
        public void Import()
        {
            var tb = new DAL.StockInfoDAL().GetSecuritiesInformation();
            int nowCount=0;
            foreach (DataRow row in tb.Rows)
            {
                var db = DBManager.GetCmsEmtity();
                string secid = Tools.GetString(row["ob_secid_0007"]);
                SecuritiesInformation c = null;
                var flag = false;
                if (!db.SecuritiesInformation.Any(x => x.Secid == secid))
                {
                    c = new SecuritiesInformation();
                    flag = true;
                    c.Id = Tools.GetLong(row["ob_object_id"]).Value;
                }
                else
                {
                    c = db.SecuritiesInformation.Where(x => x.Secid == secid).First();
                }
                SetValue(c,"Secid", Tools.GetString(row["ob_secid_0007"]));
                SetValue(c,"SecuritiesCode",Tools.GetString(row["ob_seccode_0007"]));
                SetValue(c, "ShortName", Tools.GetString(row["ob_secname_0007"]));
                SetValue(c, "PinyinReferred", Tools.GetString(row["f001v_0007"]));
                SetValue(c, "FullName", Tools.GetString(row["f016v_0007"]));
                SetValue(c, "SecuritiesClassCode", Tools.GetString(row["f002v_0007"]));
                SetValue(c, "SecuritiesClass", Tools.GetString(row["f003v_0007"]));
                SetValue(c, "MarketCode", Tools.GetString(row["f005v_0007"]));
                SetValue(c, "Market", Tools.GetString(row["f006v_0007"]));
                SetValue(c, "ListingDate", Tools.GetDateTime(row["f007d_0007"]));
                SetValue(c, "EndListingDate", Tools.GetDateTime(row["f008d_0007"]));
                SetValue(c, "FirstCount", Tools.GetDecimal(row["f009n_0007"]));
                SetValue(c, "CodeAttributeCode", Tools.GetString(row["f012v_0007"]));
                SetValue(c, "CodeAttribute", Tools.GetString(row["f013v_0007"]));
                SetValue(c, "IssuersId", Tools.GetString(row["f014v_0007"]));
                SetValue(c, "IssuersName", Tools.GetString(row["f015v_0007"]));
                SetValue(c, "ListingStatusCodes", Tools.GetString(row["f017v_0007"]));
                SetValue(c, "ListingStatus", Tools.GetString(row["f018v_0007"]));
                SetValue(c, "Memo", Tools.GetString(row["ob_memo_0007"]));
                SetValue(c, "ModifyTime", Tools.GetDateTime(row["ob_modtime_0007"]));
                SetValue(c, "EntryTime", Tools.GetDateTime(row["ob_rectime_0007"]));
                SetValue(c, "IsEnable", Tools.GetString(row["ob_isvalid_0007"]));
                if (flag)
                {
                    db.SecuritiesInformation.AddObject(c);
                }
                ImportAgencyInformation(c.Secid, db);
                ImprtBasicInfoCompanies(c.Secid, db);
                db.SaveChanges();
                nowCount++;
            }
        }

        void SetValue(object o, string p, object value)
        {
            var property=o.GetType().GetProperty(p);
            var getValue = property.GetValue(o, null);
            if (getValue != null && value != null)
            {
                if (property.GetValue(o, null).ToString() != value.ToString())
                {
                    property.SetValue(o, value, null);
                }
            }
            else
            {
                property.SetValue(o, value, null);
            }
        }

        void ImportAgencyInformation(string id,CMSEntities db)
        {
            StockInfoDAL dal = new StockInfoDAL();
            DataRow row= dal.GetAgencyInformationById(id);
            if (row == null)
                return;
            string oId = Tools.GetString(row["ob_orgid_0001"]);
            AgencyInformation c = null;
            var flag = false;
            if (!db.AgencyInformation.Any(x => x.OrgId == oId))
            {
                flag = true;
                c = new AgencyInformation();
                c.Id = Tools.GetLong(row["ob_object_id"]).Value;
            }
            else
            {
                c = db.AgencyInformation.Where(x => x.OrgId == oId).First();
            }
            SetValue(c, "OrgId", Tools.GetString(row["ob_orgid_0001"]));
            SetValue(c, "FullName", Tools.GetString(row["ob_orgname_0001"]));
            SetValue(c, "ShortName", Tools.GetString(row["f022v_0001"]));
            SetValue(c, "ListingWithin", Tools.GetString(row["f001c_0001"]));
            SetValue(c, "InstitutionTypeCode", Tools.GetString(row["f002v_0001"]));
            SetValue(c, "InstitutionType", Tools.GetString(row["f003v_0001"]));
            SetValue(c, "LegalRepresentative", Tools.GetString(row["f004v_0001"]));
            SetValue(c, "RegisteredAddress", Tools.GetString(row["f005v_0001"]));
            SetValue(c, "OfficeAddress", Tools.GetString(row["f006v_0001"]));
            SetValue(c,"ZipCode",Tools.GetString(row["f007v_0001"]));
            SetValue(c,"RegisteredCapital",Tools.GetDecimal(row["f008n_0001"]));
            SetValue(c, "CurrencyCode", Tools.GetString(row["f009v_0001"]));
            SetValue(c, "Currency", Tools.GetString(row["f010v_0001"]));
            SetValue(c, "DateEstablishment", Tools.GetDateTime(row["f011d_0001"]));
            SetValue(c, "AgencyWebsite", Tools.GetString(row["f012v_0001"]));
            SetValue(c, "Email", Tools.GetString(row["f013v_0001"]));
            SetValue(c, "TelPhone", Tools.GetString(row["f014v_0001"]));
            SetValue(c, "Fax", Tools.GetString(row["f015v_0001"]));
            SetValue(c, "MainBusiness", Tools.GetString(row["f016v_0001"]));
            SetValue(c, "Business", Tools.GetString(row["f017v_0001"]));
            SetValue(c, "NatureBusinessCode", Tools.GetString(row["f019v_0001"]));
            SetValue(c, "NatureBusiness", Tools.GetString(row["f020v_0001"]));
            SetValue(c, "OrganizationBrief", Tools.GetString(row["f021v_0001"]));
            SetValue(c, "Memo", Tools.GetString(row["ob_memo_0001"]));
            SetValue(c, "ModifyTime", Tools.GetDateTime(row["ob_modtime_0001"]));
            SetValue(c, "EntryTime", Tools.GetDateTime(row["ob_rectime_0001"]));
            SetValue(c,"IsEnable",Tools.GetString(row["ob_isvalid_0001"]));
            if (flag)
                db.AgencyInformation.AddObject(c);
        }

        void ImprtBasicInfoCompanies(string id, CMSEntities db)
        {
            try
            {
                StockInfoDAL dal = new StockInfoDAL();
                DataRow row = dal.GetBasicInfoCompanies(id);
                if (row == null)
                {
                    return;
                }
                var flag = false;
                BasicInfoCompanies c = null;
                if (!db.AgencyInformation.Any(x => x.OrgId == id))
                {
                    flag = true;
                    c = new BasicInfoCompanies();
                    c.Id = Tools.GetLong(row["ob_object_id"]).Value;
                }
                else
                {
                    c = db.BasicInfoCompanies.Where(x => x.Orgid == id).First();
                }
                SetValue(c, "Orgid", Tools.GetString(row["ob_orgid_0002"]));
                SetValue(c, "OrganizationName", Tools.GetString(row["ob_orgname_0002"]));
                SetValue(c, "ShortName", Tools.GetString(row["f001v_0002"]));
                SetValue(c, "EnglishName", Tools.GetString(row["f002v_0002"]));
                SetValue(c, "EnglishShortName", Tools.GetString(row["f003v_0002"]));
                SetValue(c, "LegalRepresentative", Tools.GetString(row["f004v_0002"]));
                SetValue(c, "RegisteredAddress", Tools.GetString(row["f005v_0002"]));
                SetValue(c, "OfficeAddress", Tools.GetString(row["f006v_0002"]));
                SetValue(c, "ZipCode", Tools.GetString(row["f007v_0002"]));
                SetValue(c, "RegisteredCapital", Tools.GetDecimal(row["f008n_0002"]));
                SetValue(c, "CurrencyCode", Tools.GetString(row["f009v_0002"]));
                SetValue(c, "Currency", Tools.GetString(row["f010v_0002"]));
                SetValue(c, "DateEfEstablishment", Tools.GetDateTime(row["f011d_0002"]));
                SetValue(c, "AgencyWebsite", Tools.GetString(row["f012v_0002"]));
                SetValue(c, "Email", Tools.GetString(row["f013v_0002"]));
                SetValue(c, "TelPhone", Tools.GetString(row["f014v_0002"]));
                SetValue(c, "Fax", Tools.GetString(row["f015v_0002"]));
                SetValue(c, "MainBusiness", Tools.GetString(row["f016v_0002"]));
                SetValue(c, "Business", Tools.GetString(row["f017v_0002"]));
                SetValue(c, "BusinessCode", Tools.GetString(row["f019v_0002"]));
                SetValue(c, "NatureoBusiness", Tools.GetString(row["f020v_0002"]));
                SetValue(c, "OrganizationBrief", Tools.GetString(row["f021v_0002"]));
                SetValue(c, "TaxRegistrationNumber", Tools.GetString(row["f022v_0002"]));
                SetValue(c, "NoBusinessLicense", Tools.GetString(row["f023v_0002"]));
                SetValue(c, "Newspapers", Tools.GetString(row["f024v_0002"]));
                SetValue(c, "NewspapersCode", Tools.GetString(row["f042v_0002"]));
                SetValue(c, "NewsWebsite", Tools.GetString(row["f025v_0002"]));
                SetValue(c, "NewsWebsiteCode", Tools.GetString(row["f043v_0002"]));
                SetValue(c, "SecretaryBoard", Tools.GetString(row["f026v_0002"]));
                SetValue(c, "SecretaryPhone", Tools.GetString(row["f027v_0002"]));
                SetValue(c, "SecretaryFax", Tools.GetString(row["f028v_0002"]));
                SetValue(c, "SecretaryEmail", Tools.GetString(row["f029v_0002"]));
                SetValue(c, "SecuritiesRepresentative", Tools.GetString(row["f030v_0002"]));
                SetValue(c, "ListingStatusCodes", Tools.GetString(row["f031v_0002"]));
                SetValue(c, "ListingStatus", Tools.GetString(row["f032v_0002"]));
                SetValue(c, "ProvincialCode", Tools.GetString(row["f033v_0002"]));
                SetValue(c, "RespectiveCities", Tools.GetString(row["f034v_0002"]));
                SetValue(c, "PoliticalSubdivision", Tools.GetString(row["f047v_0002"]));
                SetValue(c, "IndustryCode", Tools.GetString(row["f037v_0002"]));
                SetValue(c, "Sectors", Tools.GetString(row["f038v_0002"]));
                SetValue(c, "Sectors2", Tools.GetString(row["f039v_0002"]));
                SetValue(c, "Sectors3", Tools.GetString(row["f040v_0002"]));
                SetValue(c, "Sectors4", Tools.GetString(row["f041v_0002"]));
                SetValue(c, "CertifiedPublicAccountants", Tools.GetString(row["F044v_0002"]));
                SetValue(c, "LawFirm", Tools.GetString(row["F045v_0002"]));
                SetValue(c, "GeneralManage", Tools.GetString(row["F046v_0002"]));
                SetValue(c, "Memo", Tools.GetString(row["ob_memo_0002"]));
                SetValue(c, "ModifyTime", Tools.GetDateTime(row["ob_modtime_0002"]));
                SetValue(c, "EntryTime", Tools.GetDateTime(row["ob_rectime_0002"]));
                SetValue(c, "IsEnable", Tools.GetString(row["ob_isvalid_0002"]));
                if (flag)
                    db.BasicInfoCompanies.AddObject(c);
            }
            catch
            {
            }
        }



    }
}